<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">

      <el-row>
        <el-col :span="12">
          <el-form-item label="表格名称" prop="name">
            <el-input v-model="form.name" placeholder=""></el-input>
          </el-form-item>
        </el-col>


        <el-table ref="tables1" :data="form.ucQuoProjects" :row-key="row => row.id">

          <el-table-column label="序号" align="center" prop="indexNo" width="80">
            <template slot-scope="scope">
              <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].indexNo'"
                            :rules="{required: true,message: '请填写序号',trigger: 'blur',}">
                <el-input size="small" placeholder="序号" v-model="scope.row.indexNo" @change="changeIndex(scope.$index)"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="指标类型" align="center" prop="quotaType" width="80">
            <template slot-scope="scope">
              <el-form-item label="" label-width="0px">
                <dict-tag :options="dict.type.quota_type" :value="scope.row.quotaType"/>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="项目" align="center" prop="name" width="200">
            <template slot-scope="scope">
              <el-form-item label="" label-width="0px">
                {{scope.row.name}}
                <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteQuota(scope.$index)"></el-button>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="明细" align="center" prop="ucQuoProjectItems">
            <template slot-scope="scope">
              <el-row type="flex" align="middle" v-if="scope.row.chooseType ==1" >
                <el-col :span="12">
                  <el-form-item label="" label-width="0px">
                  </el-form-item>
                </el-col>
                <el-col :span="3">
                  <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].ucQuoProjectItems[0].quotaFrom'"
                                :rules="{required: true,message: '请选择积分方式',trigger: 'blur',}">
                    <el-select size="small" v-model="scope.row.ucQuoProjectItems[0].quotaFrom" clearable placeholder="积分方式" style="width: 90%;">
                      <el-option
                        v-for="dict in dict.type.quota_from"
                        :key="dict.value"
                        :label="dict.label"
                        :value="parseInt(dict.value)"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="3">
                  <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].ucQuoProjectItems[0].score'"
                                :rules="{required: true,message: '请填写分值',trigger: 'blur',}">
                    <el-input-number size="small" placeholder="分值" v-model="scope.row.ucQuoProjectItems[0].score"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="3">
                  <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].ucQuoProjectItems[0].limitType'"
                                :rules="{required: true,message: '请选择限制类型',trigger: 'blur',}">
                    <el-select size="small" v-model="scope.row.ucQuoProjectItems[0].limitType" clearable placeholder="限制类型" style="width: 90%;">
                      <el-option
                        v-for="dict in dict.type.limit_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="parseInt(dict.value)"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="3" v-if="scope.row.ucQuoProjectItems[0].limitType && scope.row.ucQuoProjectItems[0].limitType != 10">
                  <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].ucQuoProjectItems[0].limitMax'"
                                :rules="{required: true,message: '请填写频次',trigger: 'blur',}">
                    <el-input-number :min="1" :precision="0" size="small" placeholder="频次" v-model="scope.row.ucQuoProjectItems[0].limitMax"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row v-if="scope.row.chooseType ==2 ||scope.row.chooseType ==3">
                <el-col :span="24" v-for="(ucQuoProjectItem,index) in scope.row.ucQuoProjectItems" :key="'b'+ucQuoProjectItem.id">
                  <el-col :span="12" style="text-align: left;">
                    <el-form-item label="" label-width="0px">
                      {{ucQuoProjectItem.indexStr + ' ' +ucQuoProjectItem.name}}
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].ucQuoProjectItems['+index+'].quotaFrom'"
                                  :rules="{required: true,message: '请选择积分方式',trigger: 'blur',}">
                      <el-select size="small" v-model="scope.row.ucQuoProjectItems[index].quotaFrom" clearable placeholder="积分方式" style="width: 90%;">
                        <el-option
                          v-for="dict in dict.type.quota_from"
                          :key="dict.value"
                          :label="dict.label"
                          :value="parseInt(dict.value)"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].ucQuoProjectItems['+index+'].score'"
                                  :rules="{required: true,message: '请填写分值',trigger: 'blur',}">
                      <el-input-number size="small" placeholder="分值" v-model="scope.row.ucQuoProjectItems[index].score"></el-input-number>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3">
                    <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].ucQuoProjectItems['+index+'].limitType'"
                                  :rules="{required: true,message: '请选择限制类型',trigger: 'blur',}">
                      <el-select size="small" v-model="scope.row.ucQuoProjectItems[index].limitType" clearable placeholder="限制类型" style="width: 90%;">
                        <el-option
                          v-for="dict in dict.type.limit_type"
                          :key="dict.value"
                          :label="dict.label"
                          :value="parseInt(dict.value)"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="3"  v-if="scope.row.ucQuoProjectItems[index].limitType && scope.row.ucQuoProjectItems[index].limitType != 10">
                    <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].ucQuoProjectItems['+index+'].limitMax'"
                                  :rules="{required: true,message: '请填写频次',trigger: 'blur',}">
                      <el-input-number :min="1" :precision="0" size="small" placeholder="频次" v-model="scope.row.ucQuoProjectItems[index].limitMax"></el-input-number>
                    </el-form-item>
                  </el-col>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column label="说明" align="center" prop="content" width="200">
            <template slot-scope="scope">
              <el-form-item label="" label-width="0px" :prop="'ucQuoProjects['+scope.$index+'].content'"
                            :rules="{required: true,message: '请填写说明',trigger: 'blur',}">
                <el-input type="textarea" v-model="scope.row.content" placeholder=""></el-input>
              </el-form-item>
            </template>
          </el-table-column>
        </el-table>

      </el-row>

      <div style="height: 60px;"></div>
      <el-row class="fixed-button">
        <el-button @click="toAddQuota">项 目</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </el-row>

    </el-form>


    <el-dialog title="项目列表" :visible.sync="open" width="70%" append-to-body>

      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px"  @submit.native.prevent @keyup.enter.native="handleQuery">
        <el-form-item label="项目" prop="qKey">
          <el-input v-model="queryParams.qKey" placeholder="" clearable></el-input>
        </el-form-item>
        <el-form-item label="指标类型" prop="qType">
          <el-select v-model="queryParams.qType" clearable placeholder="">
            <el-option
              v-for="dict in dict.type.quota_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择类型" prop="qType1">
          <el-select v-model="queryParams.qType1" clearable placeholder="">
            <el-option
              v-for="dict in dict.type.choose_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="tables" v-loading="loading" :data="dataList">
        <el-table-column label="项目" align="center" prop="name"/>
        <el-table-column label="指标类型" align="center" prop="quotaType" sortable="custom">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.quota_type" :value="scope.row.quotaType"/>
          </template>
        </el-table-column>
        <el-table-column label="选择类型" align="center" prop="chooseType" sortable="custom">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.choose_type" :value="scope.row.chooseType"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="addQuota(scope.row)"
              v-if="quotaIds.indexOf(scope.row.id)<0"
            >添加</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </el-dialog>

  </div>
</template>
<script>



export default {
  name : "UcQuoTableAdd",
  dicts: [
    'quota_type','choose_type','limit_type','quota_from'
  ],
  data() {

    return {
      form: {
        id:0,
        name:undefined,
        isUsed : 0,
        ucQuoProjects:[]
      },
      rules: {
        name: [{ required: true, message: "请填写表格名称", trigger: "blur" }],
      },

      total: 0,
      loading: true,
      open: false,
      dataList: [],
      queryParams:{
        qKey: "",
        qType: undefined,
        qType1: undefined,
        pageNum: 1,
        pageSize: 10,
        isAsc:undefined,
        orderByColumn:undefined
      },
      quotaIds:[],


    }
  },
  created() {
    let tableId = this.$route.params && this.$route.params.tableId
    this.form.id=parseInt(tableId)
    this.getList();
    if(this.form.id && this.form.id!=0){
      this.getTable()
    }
  },
  methods: {
    getTable(){
      this.request({
        url: `/userConfig/ucQuoTable/getTable/${this.form.id}`,
        method: 'get',
      }).then(response => {
        this.form = response.data
        this.getQuotaIds()
      });
    },
    getList() {
      this.loading = true;
      this.request({
        url: '/userConfig/ucQuoQuota/list',
        method: 'get',
        params: this.queryParams
      }).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.isAsc = undefined;
      this.queryParams.orderByColumn = undefined;
      this.queryParams.pageNum = 1;
      this.$refs.tables.clearSort();
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    toAddQuota(){
      this.open = true
    },
    addQuota(row){
      this.request({
        url: '/userConfig/ucQuoQuota/getOptions',
        method: 'post',
        data: row
      }).then(response => {
        let ucQuoProject = {
          indexNo: 1,
          quotaId:  row.id,
          name:row.name,
          quotaType:row.quotaType,
          chooseType:row.chooseType,
          quotaFrom:row.quotaFrom,
          ucQuoProjectItems: [{
            score: undefined,
            limitType: undefined,
            limitMax: undefined,
          }]
        }
        if(response.data && response.data.length > 0){
          ucQuoProject.ucQuoProjectItems = response.data
        }
        this.form.ucQuoProjects.push(ucQuoProject)
        this.setUcQuoProjectNo()
        this.getQuotaIds()
        this.open = false
        console.log(this.form)
      });
    },
    deleteQuota(index){
      this.form.ucQuoProjects.splice(index,1)
      this.setUcQuoProjectNo()
      this.getQuotaIds()
    },
    ucQuoProjectSort(){
      this.form.ucQuoProjects.sort((a,b) =>{
        return a.indexNo - b.indexNo
      })
    },
    setUcQuoProjectNo(){
      console.log(this.form.ucQuoProjects)
      for (let i = 0; i < this.form.ucQuoProjects.length; i++) {
        this.$set(this.form.ucQuoProjects[i],'indexNo',i+1)
      }
      console.log(this.form.ucQuoProjects)
    },
    changeIndex(index){
      if(!this.form.ucQuoProjects[index].indexNo){
        this.$set(this.form.ucQuoProjects[index],'indexNo',0)
      }
      this.ucQuoProjectSort()
      this.setUcQuoProjectNo()
    },
    getQuotaIds(){
      this.quotaIds = this.form.ucQuoProjects.map(item => item.quotaId)
      console.log(this.quotaIds)
    },

    /** 提交按钮 */
    submitForm: function() {
      if(this.form.ucQuoProjects && this.form.ucQuoProjects.length > 0){
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (!this.form.id || this.form.id==0) {
              this.request({
                url: '/userConfig/ucQuoTable/add',
                method: 'post',
                data: this.form
              }).then(response => {
                this.$modal.msgSuccess("新增成功");
                let obj = { path: "/userConfig/ucQuoTable" };
                this.$tab.closeOpenPage(obj);
              });
            } else {
              this.request({
                url: '/userConfig/ucQuoTable/edit',
                method: 'post',
                data: this.form
              }).then(response => {
                this.$modal.msgSuccess("修改成功");
                let obj = { path: "/userConfig/ucQuoTable" };
                this.$tab.closeOpenPage(obj);
              });
            }
          }
        });
      }else{
        this.$message.error("未选择项目")
      }

    },
  }
}
</script>
<style>
.font-bolder {
  font-weight: bolder;
  color: #1890ff;
}

.el-input[type="text"] {
  width: 80%;
  height: 80%;
}

.el-cascader {
  width: 80%;
}

.is-disabled .el-input__inner{
  background: white !important;
  color: black !important;
}
.is-disabled .el-textarea__inner{
  background: white !important;
  color: black !important;
}

.fixed-button{
  position: fixed;
  bottom: 20px;
  right: 20px;
}

</style>



